package com.stylefeng.guns.modular.customer.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.customer.dao.CustomerCompanyDao;
import com.stylefeng.guns.modular.customer.service.ICustomerCompanyService;
import com.stylefeng.guns.modular.customer.service.ICustomerRelationService;
import com.stylefeng.guns.modular.system.model.CustomerCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

/**
 * 客户关联关系控制器
 *
 * @author fengshuonan
 * @Date 2018-04-14 12:00:06
 */
@Controller
@RequestMapping("/customerRelation")
public class CustomerRelationController extends BaseController {

    private String PREFIX = "/customer/customerRelation/";

    @Autowired
    private ICustomerRelationService customerRelationService;

    @Autowired
    private CustomerCompanyDao customerCompanyDao;

    @Autowired
    private ICustomerCompanyService customerCompanyService;

    /**
     * 跳转到客户关联关系
     */
    @RequestMapping(value = "/customer_relation/{customerId}")
    public String customerRelation(@PathVariable("customerId") Integer customerId, Model model) {
        if (ToolUtil.isEmpty(customerId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("customerId", customerId);
        model.addAttribute("customerName", ConstantFactory.me().getCustomerCompanyName(customerId));
        return PREFIX + "/customer_relation.html";
    }


    @RequestMapping(value = "/customer_relation_detail/{customerId}")
    public String customerRelationDetail(@PathVariable("customerId") Integer customerId,Model model) {
        if (ToolUtil.isEmpty(customerId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("relationCustomers", customerCompanyService.getRelations(customerId));
        return PREFIX + "/customer_relation_detail.html";
    }

    /**
     * 获取客户tree列表
     */
    @RequestMapping(value = "/relationTreeList/{customerId}")
    @ResponseBody
    public List<ZTreeNode> menuTreeListByRoleId(@PathVariable Integer customerId) {
        Set<Long> relationCustomerIds = this.customerRelationService.getRelationCustomerIds(customerId);
        List<ZTreeNode> customerTreeList = this.customerCompanyDao.tree(null);
        if (ToolUtil.isNotEmpty(relationCustomerIds) && ToolUtil.isNotEmpty(customerTreeList)) {
            for (ZTreeNode node : customerTreeList) {
                if (relationCustomerIds.contains(node.getId())) {
                    node.setChecked(true);
                }
            }
        }
        return customerTreeList;
    }

    /**
     * 关联客户
     */
    @RequestMapping("/setRelation")
    @ResponseBody
    public Tip setRelation(@RequestParam("customerId") Integer customerId, @RequestParam("relationIds") String relationIds) {
        if (ToolUtil.isOneEmpty(relationIds)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }

        this.customerRelationService.setRelation(customerId, relationIds);
        return SUCCESS_TIP;
    }


    /**
     * 取消关联
     */
    @RequestMapping("/unRelation/{customerId}/to/{relationId}")
    @ResponseBody
    public Tip unRelation(@PathVariable("customerId") Integer customerId, @PathVariable("relationId") Integer relationId) {
        this.customerRelationService.unRelation(customerId, relationId);
        return SUCCESS_TIP;
    }



    @RequestMapping("/list")
    @ResponseBody
    public List<CustomerCompany> list(@RequestParam(value = "customerCompanyId",required = true) Integer customerCompanyId) {
        return customerCompanyService.getRelations(customerCompanyId);
    }

}
